<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

include "base.php";
include CONFIG_PATH . 'errors.php';

class enquiry_controller extends baseController {

    public $enquiry;
    public $key;
    public $data;
    public $mod;
    public $permission;
    public $enq_display;
    public $enq_detail_display;
    public $errors;
    public $enquiries;
    public $date_time;

    public function __construct() {
        parent::__construct();
        $this->mod = new model();

        // set keyfields used for add/update modes
        $this->mod->keyFields = array("name", "admtoclass", "father");
        $this->mod->moduleName = "enquiry";

        $this->enquiry = new enquiry_model();
        $this->errors = new errors();
        //$permission = $this->enquiry->get_permissions();
        $this->permission = $this->mod->get_permissions($this->tables->table2, $this->enquiry->moduleName);
        //This is the code for navbar
        $this->enquiries = "active";
    }

    public function new_enquiry() {
        //To get datetime 
        if (isset($_GET['datetime'])) {
            $this->date_time = $this->date_time();
            echo $this->date_time;
            //    echo json_encode($this->date_time);
            exit();
        }
//This  code is to add enquiry if user have add permission
        if (isset($_GET['add'])) {

            if (($_SESSION['Addd'] == "Yes") or ( $_SESSION['admin'] == 1)) {

                $this->show("enquiry/add_enquiry");

                if ($_POST) {
                    $reslt = $this->mod->add($this->tables->table3, $_POST);

                    if ($reslt) {

                        $msg = $this->errors->insertion_successful;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                    } else {

                        $msg = $this->errors->default_failed;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                    }
                }
            } else {
                //if user doesnt have add permission then this code will run
                $msg = $this->errors->access_denied;
                echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                echo '<SCRIPT LANGUAGE="JavaScript">
                         window.location="enquiry.php";
                        </script>';
            }
            exit();
        }
        //This  code is edit enquiry if user have edit permission
        if (isset($_GET['edit'])) {
            //  $this->permission = $this->mod->get_permissions($this->tables->table2, "Edit", "enquiry");
            if (($_SESSION['Edit'] == "Yes") or ( $_SESSION['admin'] == 1)) {
                $this->data = $_GET['edit'];
                $this->enq_detail_display = $this->enquiry->enq_detail_display($this->tables->table3, $this->data);
                $this->show("enquiry/enquiry_edit");
                if ($_POST) {
                    $update = $this->mod->update($this->tables->table3, $this->data, "enqno", $_POST);

                    if ($update) {
                        $msg = $this->errors->update_successful;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                             window.location.reload();
                      </SCRIPT>");
                    } else {
                        $msg = $this->errors->default_failed;
                        echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                    }
                }
            } else {
                //if user doesnt have edit permission then this code will run
                $msg = $this->errors->access_denied;
                echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                echo '<SCRIPT LANGUAGE="JavaScript">
                         window.location="enquiry.php";
                        </script>';
            }

            exit();
        }
        //This  code is delete enquiry if user have delete permission
        if ($_POST) {
            if (($_SESSION['Del'] == "Yes") or ( $_SESSION['admin'] == 1)) {
                $reslt = $this->mod->delete($this->tables->table3, $_POST['chkDel'], "enqno");
                if ($reslt) {
                    $msg = $this->errors->delete_successful;
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');             
                      window.location.reload();
                      </SCRIPT>");
                } else {

                    $msg = $this->errors->default_failed;
                    echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                }
            } else {
                //if user doesnt have delete permission then this code will run
                $msg = $this->errors->access_denied;
                echo ("<SCRIPT LANGUAGE='JavaScript'>
                       window.alert('$msg');
                      </SCRIPT>");
                echo '<SCRIPT LANGUAGE="JavaScript">
                         window.location="enquiry.php";
                        </script>';
            }
        }
        //This  code is to show all enquiry records
        if (isset($_GET['show_all'])) {
            $this->enq_display = $this->enquiry->enquiry_display($this->tables->table3);
            foreach ($this->enq_display as $key => $value) {
                ?>
                <tr>
                    <td><input type="checkbox" name="chkDel[]" id="chkDel<?= $i; ?>" value="<?php echo $value['enqno']; ?>"></td>
                    <td><?php echo $value['enqno']; ?></td>
                    <td><?php echo $value['enqdate']; ?></td>                
                    <td><?php
                if ($_SESSION['Edit'] == "Yes") {
                    echo '<a href=?edit=' . $value['enqno'] . '>' . $value['name'] . '</a>';
                } else {
                    echo $value['name'];
                }
                ?>            
                    </td>
                    <td><?php echo $value['father']; ?></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td><?php echo $value['admtoclass']; ?></td>
                    <td><?php echo $value['caddress']; ?></td>
                    <td><?php echo $value['cstreet']; ?></td>
                    <td><?php echo $value['carea']; ?></td>
                    <td>&nbsp;</td>

                </tr>
                <?php
            }
            exit();
        } else if (isset($_GET['show_recent'])) {
            //This  code is to show recent (10 or n) no of records
            $this->enq_display = $this->enquiry->enquiry_display_recent($this->tables->table3);
            foreach ($this->enq_display as $key => $value) {
                ?>
                <tr>
                    <td><input type="checkbox" name="chkDel[]" id="chkDel<?= $i; ?>" value="<?php echo $value['enqno']; ?>"></td>
                    <td><?php echo $value['enqno']; ?></td>
                    <td><?php echo $value['enqdate']; ?></td>                
                    <td><?php
                if ($_SESSION['Edit'] == "Yes") {
                    echo '<a href=?edit=' . $value['enqno'] . '>' . $value['name'] . '</a>';
                } else {
                    echo $value['name'];
                }
                ?>            
                    </td>
                    <td><?php echo $value['father']; ?></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td><?php echo $value['admtoclass']; ?></td>
                    <td><?php echo $value['caddress']; ?></td>
                    <td><?php echo $value['cstreet']; ?></td>
                    <td><?php echo $value['carea']; ?></td>
                    <td>&nbsp;</td>

                </tr>
                <?php
            }
            exit();
        } else {
            $this->enq_display = $this->enquiry->enquiry_display_recent($this->tables->table3);
            $this->show("enquiry/index");
        }
    }

}

$enquiryc = new enquiry_controller();
$enquiryc->new_enquiry();
?>